//    Copyright (C) Kherty.  All rights reserved.
using System;
using System.Diagnostics;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Threading;

namespace OpenLS.Spreadsheet.Debugging
{
#if DEBUG
    class FocusHelper
    {
        public static void StartShowFocusTimer(Window window)
        {
            Dispatcher dispatcher = window.Dispatcher;
           //\\ return;
            DispatcherTimer t = new DispatcherTimer(
    new TimeSpan(0, 0, 5), DispatcherPriority.Background, delegate
 {

     //\\return;
     FrameworkElement e = Keyboard.FocusedElement as FrameworkElement;
     if (e == null)
     {
         Debug.WriteLine("no focus");
         return;
     }
     Debug.WriteLine("//////////////FocusManager.GetFocusedElement: " + FocusManager.GetFocusedElement(window));
     Debug.Write(e.Name + " ");
     Debug.Write("|");
     Debug.Write(e.IsMouseOver + " ");
     Debug.Write("|");
     Debug.Write(e.IsMouseDirectlyOver + " ");
     Debug.Write("|");
     Debug.Write(e);
     if (e is ContentControl)
     {
         ContentControl cc = (ContentControl)Keyboard.FocusedElement;
         Debug.Write(cc.Content);
         Debug.Write("|");
         Debug.Write(cc.ActualWidth);
         Debug.Write("|");
         Debug.Write(cc.ActualHeight);
         Debug.Write("|");
     }
     if (e is ContentPresenter)
     {
         ContentPresenter cc = (ContentPresenter)Keyboard.FocusedElement;
         Debug.Write(cc.Content);
         Debug.Write("|");
         Debug.Write(cc.ActualWidth);
         Debug.Write("|");
         Debug.Write(cc.ActualHeight);
         Debug.Write("|");
     } FrameworkElement p = e.Parent as FrameworkElement;
     while (p != null)
     {
         Debug.Write(p);
         Debug.Write("|");
         Debug.Write(p.Name);
         Debug.Write("|");
         p = p.Parent as FrameworkElement;
     }
     Debug.WriteLine("");
     Debug.WriteLine("-----------------------");
 }, dispatcher);
            t.Start();
        }
        public static string CurrentFocusText
        {
            get
            {
                StringBuilder result = new StringBuilder();
                FrameworkElement e = Keyboard.FocusedElement as FrameworkElement;
                if (e == null)
                {
                    result.Append("no focus");
                    return result.ToString();
                }
                result.Append(e.Name + " ");
                result.Append("|");
                result.Append(e.IsMouseOver + " ");
                result.Append("|");
                result.Append(e.IsMouseDirectlyOver + " ");
                result.Append("|");
                result.Append(e);
                if (e is ContentControl)
                {
                    ContentControl cc = (ContentControl)Keyboard.FocusedElement;
                    result.Append(cc.Content);
                    result.Append("|");
                    result.Append(cc.ActualWidth);
                    result.Append("|");
                    result.Append(cc.ActualHeight);
                    result.Append("|");
                }
                if (e is ContentPresenter)
                {
                    ContentPresenter cc = (ContentPresenter)Keyboard.FocusedElement;
                    result.Append(cc.Content);
                    result.Append("|");
                    result.Append(cc.ActualWidth);
                    result.Append("|");
                    result.Append(cc.ActualHeight);
                    result.Append("|");
                } FrameworkElement p = e.Parent as FrameworkElement;
                while (p != null)
                {
                    result.Append(p);
                    result.Append("|");
                    result.Append(p.Name);
                    result.Append("|");
                    p = p.Parent as FrameworkElement;
                }
                result.Append("");
                result.Append("-----------------------");
                return result.ToString();
            }
        }
    }
#endif
}
